home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 198_01 / file.c < prev    next >
C/C++ Source or Header  |  1990-01-21  |  23KB  |  769 lines

  1. /*    FILE.C:   for MicroEMACS
  2.  
  3.     The routines in this file handle the reading, writing
  4.     and lookup of disk files.  All of details about the
  5.     reading and writing of the disk are in "fileio.c".
  6.  
  7. */
  8.  
  9. #include        <stdio.h>
  10. #include    "estruct.h"
  11. #include        "edef.h"
  12.  
  13. /*
  14.  * Read a file into the current
  15.  * buffer. This is really easy; all you do it
  16.  * find the name of the file, and call the standard
  17.  * "read a file into the current buffer" code.
  18.  * Bound to "C-X C-R".
  19.  */
  20. fileread(f, n)
  21. {
  22.         register int    s;
  23.         char fname[NFILEN];
  24.  
  25.     if (restflag)        /* don't allow this command if restricted */
  26.         return(resterr());
  27.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  28.                 return(s);
  29.         return(readin(fname, TRUE, FALSE));
  30. }
  31.  
  32. /*
  33.  * Insert a file into the current
  34.  * buffer. This is really easy; all you do it
  35.  * find the name of the file, and call the standard
  36.  * "insert a file into the current buffer" code.
  37.  * Bound to "C-X C-I".
  38.  */
  39. insfile(f, n)
  40. {
  41.         register int    s;
  42.         char fname[NFILEN];
  43.  
  44.     if (restflag)        /* don't allow this command if restricted */
  45.         return(resterr());
  46.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  47.         return(rdonly());    /* we are in read only mode    */
  48.         if ((s=mlreply("Insert file: ", fname, NFILEN)) != TRUE)
  49.                 return(s);
  50.         return(ifile(fname));
  51. }
  52.  
  53. /*
  54.  * Select a file for editing.
  55.  * Look around to see if you can find the
  56.  * fine in another buffer; if you can find it
  57.  * just switch to the buffer. If you cannot find
  58.  * the file, create a new buffer, read in the
  59.  * text, and switch to the new buffer.
  60.  * Bound to C-X C-F.
  61.  */
  62. filefind(f, n)
  63. {
  64.         char fname[NFILEN];    /* file user wishes to find */
  65.         register int s;        /* status return */
  66.  
  67.     if (restflag)        /* don't allow this command if restricted */
  68.         return(resterr());
  69.         if ((s=mlreply("Find file: ", fname, NFILEN)) != TRUE)
  70.                 return(s);
  71.     return(getfile(fname, TRUE));
  72. }
  73.  
  74. #if    DECEDT
  75. /*
  76.  * Select a file for editing and insert it into another window.
  77.  * If there is only one window, split it, otherwise pick a non-current window.
  78.  * Look around to see if you can find the
  79.  * file in another buffer; if you can find it
  80.  * just switch to the buffer. If you cannot find
  81.  * the file, create a new buffer, read in the
  82.  * text, and switch to the new buffer.
  83.  * Bound to C-X 4.
  84.  */
  85. filewfind(f, n)
  86. {
  87.         char fname[NFILEN];    /* file user wishes to find */
  88.         register int s;        /* status return */
  89.     register WINDOW *wp;
  90.  
  91.     if (restflag)        /* don't allow this command if restricted */
  92.         return(resterr());
  93.     if ((s=mlreply("Find file: ", fname, NFILEN)) != TRUE) return(s);
  94.  
  95.     /* look for a window */
  96.     wp = wheadp;
  97.     while (wp != NULL && wp->w_wndp != curwp)
  98.         wp = wp->w_wndp;
  99.     if (wp == NULL) wp = curwp->w_wndp;
  100.     if (wp == NULL) {
  101.         if ((s=splitwind(f, n)) != TRUE) return(s);
  102.         wp = spltwp;
  103.         }
  104.     curwp = wp;
  105.     curbp = wp->w_bufp;
  106.  
  107.     s = getfile(fname, TRUE);
  108.     return(s);
  109. }
  110.  
  111. /*
  112.  * Read the last saved version of a file into the current buffer.
  113.  * Unound.
  114.  */
  115. filerevert(f, n)
  116. {
  117.     char fname[NFILEN];
  118.  
  119.     if (restflag)        /* don't allow this command if restricted */
  120.         return(resterr());
  121.     if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  122.         return (TRUE);
  123.     if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  124.         mlwrite("No file name");
  125.         return (FALSE);
  126.     }
  127.     strcpy(fname, curbp->b_fname);
  128.     return(readin(fname, TRUE, FALSE));
  129. }
  130. #endif
  131.  
  132. viewfile(f, n)    /* visit a file in VIEW mode */
  133. {
  134.         char fname[NFILEN];    /* file user wishes to find */
  135.         register int s;        /* status return */
  136.  
  137.     if (restflag)        /* don't allow this command if restricted */
  138.         return(resterr());
  139.         if ((s=mlreply("View file: ", fname, NFILEN)) != TRUE)
  140.                 return (s);
  141.     s = getfile(fname, FALSE);
  142.     if (s) {    /* if we succeed, put it in view mode */
  143.         curwp->w_bufp->b_mode |= MDVIEW;
  144.  
  145.         /* scan through and update mode lines of all windows */
  146.         upmode();
  147.     }
  148.     return(s);
  149. }
  150.  
  151. #if    CRYPT
  152. resetkey()    /* reset the encryption key if needed */
  153.  
  154. {
  155.     register int s;    /* return status */
  156.  
  157.     /* turn off the encryption flag */
  158.     cryptflag = FALSE;
  159.  
  160.     /* if we are in crypt mode */
  161.     if (curbp->b_mode & MDCRYPT) {
  162.         if (curbp->b_key[0] == 0) {
  163.             s = setkey(FALSE, 0);
  164.             if (s != TRUE)
  165.                 return(s);
  166.         }
  167.  
  168.         /* let others know... */
  169.         cryptflag = TRUE;
  170.  
  171.         /* and set up the key to be used! */
  172.         /* de-encrypt it */
  173.         crypt((char *)NULL, (unsigned) 0);
  174.         crypt(curbp->b_key, (unsigned) strlen(curbp->b_key));
  175.  
  176.         /* re-encrypt it...seeding it to start */
  177.         crypt((char *)NULL, (unsigned) 0);
  178.         crypt(curbp->b_key, (unsigned) strlen(curbp->b_key));
  179.     }
  180.  
  181.     return(TRUE);
  182. }
  183. #endif
  184.  
  185. getfile(fname, lockfl)
  186.  
  187. char fname[];        /* file name to find */
  188. int lockfl;        /* check the file for locks? */
  189.  
  190. {
  191.         register BUFFER *bp;
  192.         register LINE   *lp;
  193.         register int    i;
  194.         register int    s;
  195.         char bname[NBUFN];    /* buffer name to put file */
  196.  
  197. #if    MSDOS
  198.     mklower(fname);        /* msdos isn't case sensitive */
  199. #endif
  200.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  201.                 if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  202.             swbuffer(bp);
  203.                         lp = curwp->w_dotp;
  204.                         i = curwp->w_ntrows/2;
  205.                         while (i-- && lback(lp)!=curbp->b_linep)
  206.                                 lp = lback(lp);
  207.                         curwp->w_linep = lp;
  208.                         curwp->w_flag |= WFMODE|WFHARD;
  209.                         mlwrite("[Old buffer]");
  210.                         return (TRUE);
  211.                 }
  212.         }
  213.         makename(bname, fname);                 /* New buffer name.     */
  214.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  215.         /* old buffer name conflict code */
  216.                 s = mlreply("Buffer name: ", bname, NBUFN);
  217.                 if (s == ABORT)                 /* ^G to just quit      */
  218.                         return (s);
  219.                 if (s == FALSE) {               /* CR to clobber it     */
  220.                         makename(bname, fname);
  221.                         break;
  222.                 }
  223.         }
  224.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  225.                 mlwrite("Cannot create buffer");
  226.                 return (FALSE);
  227.         }
  228.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  229.                 curbp->b_dotp = curwp->w_dotp;
  230.                 curbp->b_doto = curwp->w_doto;
  231.                 curbp->b_markp = curwp->w_markp;
  232.                 curbp->b_marko = curwp->w_marko;
  233.                 curbp->b_fcol  = curwp->w_fcol;
  234.         }
  235.         curbp = bp;                             /* Switch to it.        */
  236.         curwp->w_bufp = bp;
  237.         curbp->b_nwnd++;
  238.         return(readin(fname, lockfl, FALSE));   /* Read it in.          */
  239. }
  240.  
  241. /*
  242.     Read file "fname" into the current buffer, blowing away any text
  243.     found there.  Called by both the read and find commands.  Return
  244.     the final status of the read.  Also called by the mainline, to
  245.     read in a file specified on the command line as an argument. 
  246.     The command bound to M-FNR is called after the buffer is set up
  247.     and before it is read. 
  248. */
  249.  
  250. readin(fname, lockfl, recover)
  251.  
  252. char    fname[];    /* name of file to read */
  253. int    lockfl;        /* check for file locks? */
  254. int    recover;    /* recover an autosaved file */
  255.  
  256. {
  257.         register LINE   *lp1;
  258.         register LINE   *lp2;
  259.         register int    i;
  260.         register WINDOW *wp;
  261.         register BUFFER *bp;
  262.         register int    s;
  263.         register int    nbytes;
  264.         register int    nline;
  265.     char mesg[NSTRING];
  266.         char tempname[NFILEN + 4];
  267.  
  268. #if    FILOCK
  269.     if (lockfl && lockchk(fname) == ABORT)
  270.         return(ABORT);
  271. #endif
  272.         bp = curbp;                             /* Cheap.               */
  273.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  274.                 return (s);
  275.         bp->b_flag &= ~(BFINVS|BFCHG);
  276.         strcpy(bp->